//
//  SearchBarViewController.m
//  MyLearnJob
//
//  Created by qianfeng on 15-6-30.
//  Copyright (c) 2015年 朱共营. All rights reserved.
//

#import "SearchBarViewController.h"
#import "AppModel.h"
#import "MyCell.h"
#import "ParticularsViewController.h"
@interface SearchBarViewController ()<UISearchBarDelegate, UITableViewDataSource,UITableViewDelegate>
{
    UITableView *_tableView;
    NSArray *_mutArr;
    NSString *_getStr;
}
@property(nonatomic,copy) NSString *getStr;
@property(nonatomic,strong) NSArray *mutArr;
@property(nonatomic,strong)  UITableView *tableView;
@end

@implementation SearchBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor colorWithRed:233/255.f green:229/255.f blue:223/255.f alpha:1];
    self.tableView=[[UITableView alloc] initWithFrame:CGRectMake(0,0, kScreenSize.width, kScreenSize.height-64) style:UITableViewStylePlain];
    
    [self setViewUI];
    NSLog(@"%@",self.myDataArr[1]);
}
- (void)setViewUI{
    
        UISearchBar *search=[[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, kScreenSize.width-40, 50)];
        search.delegate=self;
        search.placeholder=@"课程搜索";
        self.navigationItem.titleView=search;
}
- (void)willSeachTestInData{
    self.mutArr = [self.myDataArr filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
        
        AppModel *model = (AppModel *)evaluatedObject;
        NSRange range = [model.name rangeOfString:self.getStr options:NSCaseInsensitiveSearch];
        return range.location != NSNotFound;
               
            }]];
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    [self.tableView registerNib:[UINib nibWithNibName:@"MyCell" bundle:nil] forCellReuseIdentifier:@"MyCell"];
    [self.view addSubview:self.tableView];
    NSLog(@"%@",self.mutArr);
   
    

}
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    [searchBar setShowsCancelButton:YES animated:YES];
    return YES;
}
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{
    [searchBar setShowsCancelButton:NO animated:YES];
    return YES;
    
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    [searchBar resignFirstResponder];
    searchBar.text=@"";
    self.mutArr=nil;
    [self.tableView reloadData];
    
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [searchBar resignFirstResponder];
    self.getStr=searchBar.text;
    [self willSeachTestInData];
 NSLog(@"%@",self.getStr);
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 1;

}
- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.mutArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AppModel *model=self.mutArr[indexPath.row];
    MyCell *cell=[self.tableView dequeueReusableCellWithIdentifier:@"MyCell" forIndexPath:indexPath];
    [cell showDataWithModel:model];
    NSLog(@"%@",model);
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 115;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ParticularsViewController *parti=[[ParticularsViewController alloc] init];
    AppModel *model=self.mutArr[indexPath.row];
    parti.idd=model.id;
    
    [self.navigationController pushViewController:parti animated:YES];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
